﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;

namespace QidorSpider.Components.Data
{
    public class cDataHelper
    {
        private static string sql = "";

        #region DatabaseProperty
        private static DatabaseProperty accessProperty
        {
            get
            {
                DatabaseProperty defaultProperty = new DatabaseProperty();
                defaultProperty.ConnectionString = string.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0}\\ET.mdb;",
                                                                 AppDomain.CurrentDomain.BaseDirectory.ToString());
                defaultProperty.DatabaseType = DatabaseType.OleDBSupported;
                return defaultProperty;
            }
        }

        private static DatabaseProperty mssqlProperty
        {
            get
            {
                DatabaseProperty defaultProperty = new DatabaseProperty();
                defaultProperty.ConnectionString = string.Format("server=foolnet-3fa9be8;database=WebsharpTest;uid=sa;pwd=",
                                                                 AppDomain.CurrentDomain.BaseDirectory.ToString());
                defaultProperty.DatabaseType = DatabaseType.OleDBSupported;
                return defaultProperty;
            }
        }

        private static DatabaseProperty oracleProperty
        {
            get
            {
                DatabaseProperty defaultProperty = new DatabaseProperty();
                defaultProperty.ConnectionString = string.Format("Data Source=websharp;user id=foolnet;pwd=foolnet",
                                                                 AppDomain.CurrentDomain.BaseDirectory.ToString());
                defaultProperty.DatabaseType = DatabaseType.OleDBSupported;
                return defaultProperty;
            }
        }
        #endregion

        private static DataAccess dataAccess = DataAccessFactory.CreateDataAccess(accessProperty);

        #region glist ok
        public static List<cClassData> GetClassDataList(string classType)
        {
            List<cClassData> classDataList = new List<cClassData>();

            sql = "select * from glist where g_type = @g_type";

            QueryParameterCollection collection = new QueryParameterCollection();
            collection.Add("@g_type", classType, DbType.String);

            DataTable resultTable = dataAccess.ExecuteDataset(sql, collection).Tables[0];
            if (resultTable != null && resultTable.Rows.Count > 0)
            {
                foreach(DataRow row in resultTable.Rows)
                {
                    cClassData classData = new cClassData();
                    classData.g_id = TypeConverter.ObjectToInt(row["g_id"]);
                    classData.g_name = row["g_name"].ToString();
                    classData.g_type = row["g_type"].ToString();
                    classData.g_sn = TypeConverter.ObjectToInt(row["g_sn"]);

                    classDataList.Add(classData);
                }
            }

            return classDataList;
        }

        public static int InsClassData(cClassData classData)
        {
            sql = "insert into glist(g_type,g_name,g_sn) values(@g_type,@g_name,@g_sn)";

            QueryParameterCollection collection = new QueryParameterCollection();
            collection.Add("@g_type", classData.g_type, DbType.String);
            collection.Add("@g_name", classData.g_name, DbType.String);
            collection.Add("@g_sn", classData.g_sn, DbType.Int32);

            return dataAccess.ExecuteNonQuery(sql, collection);
        }

        public static int DelClassData(int classID)
        {
            sql = "delete glist where g_id=@g_id";

            QueryParameterCollection collection = new QueryParameterCollection();
            collection.Add("@g_id", classID, DbType.Int32);

            return dataAccess.ExecuteNonQuery(sql, collection);
        }

        public static int UpdClassData(cClassData classData)
        {
            sql = "update glist set g_name=@g_name,g_type=@g_type,g_sn=@g_sn where g_id=@g_id";

            QueryParameterCollection collection = new QueryParameterCollection();
            collection.Add("@g_id", classData.g_id, DbType.Int32);
            collection.Add("@g_name", classData.g_name, DbType.String);
            collection.Add("@g_type", classData.g_type, DbType.String);
            collection.Add("@g_sn", classData.g_sn, DbType.Int32);

            return dataAccess.ExecuteNonQuery(sql, collection);
        }
        #endregion

        //#region rule_post
        //public static List<cPublishWebData> GetPublishWebDataNameList(int classID)
        //{
        //    List<cPublishWebData> publishWebDataList = new List<cPublishWebData>();

        //    sql = "select id,name from rule_post where g_id = @g_id";

        //    QueryParameterCollection collection = new QueryParameterCollection();
        //    collection.Add("@g_id", classID, DbType.Int32);

        //    DataTable publishWebDataTable = dataAccess.ExecuteDataset(sql, collection).Tables[0];
        //    if(publishWebDataTable != null && publishWebDataTable.Rows.Count > 0)
        //    {
        //        foreach(DataRow row in publishWebDataTable.Rows)
        //        {
        //            cPublishWebData publishWebData = new cPublishWebData();
        //            publishWebData.ID = TypeConverter.ObjectToInt(row["id"]);
        //            publishWebData.Name = row["name"].ToString();

        //            publishWebDataList.Add(publishWebData);
        //        }
        //    }

        //    return publishWebDataList;
        //}

        //public static cPublishWebData GetPublishWebData(int id)
        //{
        //    cPublishWebData publishWebData = new cPublishWebData();

        //    sql = "select * from rule_post where id=@id";

        //    QueryParameterCollection collection = new QueryParameterCollection();
        //    collection.Add("@id", id, DbType.Int32);

        //    IDataReader reader = dataAccess.ExecuteReader(sql, collection);
        //    while(reader.Read())
        //    {
        //        publishWebData.ID = TypeConverter.StrToInt(reader["id"].ToString());
        //        publishWebData.Name = reader["name"].ToString();
        //        publishWebData.UrlChk = reader["URLchk"].ToString();
        //        publishWebData.UrlPost = reader["URLpost"].ToString();
        //        publishWebData.UrlReply = reader["URLreply"].ToString();
        //        publishWebData.IsReply = TypeConverter.StrToBool(reader["isreply"].ToString(), false);
        //        publishWebData.LenTitleMin = TypeConverter.StrToInt(reader["Len_t_s"].ToString());
        //        publishWebData.LenTitleMax = TypeConverter.StrToInt(reader["Len_t_l"].ToString());
        //        publishWebData.LenContentMin = TypeConverter.StrToInt(reader["Len_c_s"].ToString());
        //        publishWebData.LenContentMax = TypeConverter.StrToInt(reader["len_c_l"].ToString());
        //        publishWebData.PostTitle = reader["ptitle"].ToString();
        //        publishWebData.PostContent = reader["pcontent"].ToString();
        //        publishWebData.PostUser = reader["puser"].ToString();
        //        publishWebData.PostPass = reader["ppsw"].ToString();
        //        publishWebData.PostPara1 = reader["para1"].ToString();
        //        publishWebData.Userlist = reader["userlist"].ToString();
        //        publishWebData.Paralist = reader["paralist"].ToString();
        //        publishWebData.IsFtp = TypeConverter.StrToBool(reader["FTP"].ToString(), false);
        //        publishWebData.FtpServer = reader["FTPserver"].ToString();
        //        publishWebData.FtpPort = TypeConverter.StrToInt(reader["FTPport"].ToString());
        //        publishWebData.FtpUser = reader["FTPuser"].ToString();
        //        publishWebData.FtpPass = reader["FTPpsw"].ToString();
        //        publishWebData.FtpPasv = reader["FTPpasv"].ToString();
        //        publishWebData.YmUpDir = reader["ymupdir"].ToString();
        //        publishWebData.CodePage = TypeConverter.StrToInt(reader["codepage"].ToString());
        //        publishWebData.TransLang = TypeConverter.StrToInt(reader["TransLang"].ToString());
        //        publishWebData.TransParas = reader["TransParas"].ToString();
        //        publishWebData.PathFile = reader["pathfile"].ToString();
        //        publishWebData.UrlFile = reader["URLfile"].ToString();
        //        publishWebData.PostFiles = reader["pfiles"].ToString();
        //        publishWebData.PostUrl = reader["psurl"].ToString();
        //        publishWebData.IsLoginPost = TypeConverter.StrToBool(reader["isloginpost"].ToString(), false);
        //        publishWebData.PostCookies = reader["postcookies"].ToString();
        //        publishWebData.PostOkCode = reader["postokcode"].ToString();
        //        publishWebData.PostAddUrl = reader["postaddurl"].ToString();
        //        publishWebData.IsUbb = TypeConverter.StrToBool(reader["isubb"].ToString(), false);
        //        publishWebData.IsCrlf = TypeConverter.StrToBool(reader["iscrlf"].ToString(), false);
        //        publishWebData.Sort = TypeConverter.StrToInt(reader["sn"].ToString());
        //        publishWebData.ClassID = TypeConverter.StrToInt(reader["g_id"].ToString());
        //    }

        //    return publishWebData;
        //}

        //public static int InsPublishWebData(cPublishWebData publishWebData)
        //{
        //    sql = "insert into rule_post(name,g_id) values(@name,@g_id)";

        //    QueryParameterCollection collection = new QueryParameterCollection();
        //    collection.Add("@name", publishWebData.ID, DbType.Int32);
        //    collection.Add("@g_id", publishWebData.ClassID, DbType.String);

        //    return dataAccess.ExecuteNonQuery(sql, collection);
        //}

        //public static int UpdPublishWebData(cPublishWebData publishWebData)
        //{
        //    sql = "update rule_post set name=@name,URLchk=@URLchk,URLpost=@URLpost,URLreply=@URLreply,isreply=@isreply,Len_t_s=@Len_t_s,Len_t_l=@Len_t_l,Len_c_s=@Len_c_s,len_c_l=@len_c_l,ptitle=@ptitle,pcontent=@pcontent,puser=@puser,ppsw=@ppsw,para1=@para1,userlist=@userlist,paralist=@paralist,FTP=@FTP,FTPserver=@FTPserver,FTPport=@FTPport,FTPuser=@FTPuser,FTPpsw=@FTPpsw,FTPpasv=@FTPpasv,ymupdir=@ymupdir,codepage=@codepage,TransLang=@TransLang,TransParas=@TransParas,pathfile=@pathfile,URLfile=@URLfile,pfiles=@pfiles,psurl=@psurl,isloginpost=@isloginpost,postcookies=@postcookies,postokcode=@postokcode,postaddurl=@postaddurl,isubb=@isubb,iscrlf=@iscrlf,sn=@sn,g_id=@g_id where id=@id";

        //    QueryParameterCollection collection = new QueryParameterCollection();
        //    collection.Add("@name", publishWebData.Name, DbType.String);
        //    collection.Add("@URLchk", publishWebData.UrlChk, DbType.String);
        //    collection.Add("@URLpost", publishWebData.UrlPost, DbType.String);
        //    collection.Add("@URLreply", publishWebData.UrlReply, DbType.String);
        //    collection.Add("@isreply", publishWebData.IsReply, DbType.Boolean);
        //    collection.Add("@Len_t_s", publishWebData.LenTitleMin, DbType.Int32);
        //    collection.Add("@Len_t_l", publishWebData.LenTitleMax, DbType.Int32);
        //    collection.Add("@Len_c_s", publishWebData.LenContentMin, DbType.Int32);
        //    collection.Add("@len_c_l", publishWebData.LenContentMax, DbType.Int32);
        //    collection.Add("@ptitle", publishWebData.PostTitle, DbType.String);
        //    collection.Add("@pcontent", publishWebData.PostContent, DbType.String);
        //    collection.Add("@puser", publishWebData.PostUser, DbType.String);
        //    collection.Add("@ppsw", publishWebData.PostPass, DbType.String);
        //    collection.Add("@para1", publishWebData.PostPara1, DbType.String);
        //    collection.Add("@userlist", publishWebData.Userlist, DbType.String);
        //    collection.Add("@paralist", publishWebData.Paralist, DbType.String);
        //    collection.Add("@FTP", publishWebData.IsFtp, DbType.String);
        //    collection.Add("@FTPserver", publishWebData.FtpServer, DbType.String);
        //    collection.Add("@FTPport", publishWebData.FtpPort, DbType.Int32);
        //    collection.Add("@FTPuser", publishWebData.FtpUser, DbType.String);
        //    collection.Add("@FTPpsw", publishWebData.FtpPass, DbType.String);
        //    collection.Add("@FTPpasv", publishWebData.FtpPasv, DbType.Boolean);
        //    collection.Add("@ymupdir", publishWebData.YmUpDir, DbType.Boolean);
        //    collection.Add("@codepage", publishWebData.CodePage, DbType.Int32);
        //    collection.Add("@TransLang", publishWebData.TransLang, DbType.Int32);
        //    collection.Add("@TransParas", publishWebData.TransParas, DbType.String);
        //    collection.Add("@pathfile", publishWebData.PathFile, DbType.String);
        //    collection.Add("@URLfile", publishWebData.UrlFile, DbType.String);
        //    collection.Add("@pfiles", publishWebData.PostFiles, DbType.String);
        //    collection.Add("@psurl", publishWebData.PostUrl, DbType.String);
        //    collection.Add("@isloginpost", publishWebData.IsLoginPost, DbType.Boolean);
        //    collection.Add("@postcookies", publishWebData.PostCookies, DbType.String);
        //    collection.Add("@postokcode", publishWebData.PostOkCode, DbType.String);
        //    collection.Add("@postaddurl", publishWebData.PostAddUrl, DbType.String);
        //    collection.Add("@isubb", publishWebData.IsUbb, DbType.Boolean);
        //    collection.Add("@iscrlf", publishWebData.IsCrlf, DbType.Boolean);
        //    collection.Add("@sn", publishWebData.Sort, DbType.Int32);
        //    collection.Add("@g_id", publishWebData.ClassID, DbType.Int32);
        //    collection.Add("@id", publishWebData.ID, DbType.Int32);

        //    return dataAccess.ExecuteNonQuery(sql, collection);
        //}

        //public static int DelPublishWebData(int id)
        //{
        //    sql = "delete rule_post where id=@id";

        //    QueryParameterCollection collection = new QueryParameterCollection();
        //    collection.Add("@id", id, DbType.Int32);

        //    return dataAccess.ExecuteNonQuery(sql, collection);
        //}

        //#endregion



        /* OK ***************::*/

        #region list ok

        public static List<cPageUrlData> GetPageUrlDataList(int planID)
        {
            List<cPageUrlData> pageUrlDataList = new List<cPageUrlData>();

            sql = "select * from list where planid = @planid";

            QueryParameterCollection collection = new QueryParameterCollection();
            collection.Add("@planid", planID, DbType.Int32);

            DataTable resultTable = dataAccess.ExecuteDataset(sql, collection).Tables[0];
            if(resultTable != null && resultTable.Rows.Count > 0)
            {
                foreach(DataRow row in resultTable.Rows)
                {
                    cPageUrlData pageUrlData = new cPageUrlData();
                    pageUrlData.ID = TypeConverter.StrToInt(row["id"].ToString());
                    pageUrlData.PlanID = TypeConverter.StrToInt(row["planid"].ToString());
                    pageUrlData.Title = row["title"].ToString();
                    pageUrlData.Link = row["link"].ToString();
                    pageUrlData.LitPic = row["litpic"].ToString();
                    pageUrlData.PostTime = TypeConverter.StrToDateTime(row["posttime"].ToString());
                    pageUrlData.ActionFlag = TypeConverter.StrToBool(row["actionflag"].ToString(), false);
                    pageUrlData.PostFlag = TypeConverter.StrToBool(row["postflag"].ToString(), false);
                    pageUrlData.ChkFlag = TypeConverter.StrToBool(row["chkflag"].ToString(), false);
                    pageUrlData.ErrFlag = TypeConverter.StrToInt(row["errflag"].ToString());

                    pageUrlDataList.Add(pageUrlData);
                }
            }

            return pageUrlDataList;
        }

        public static int InsPageUrlData(cPageUrlData pageUrlData)
        {
            sql = "insert into list(planid,title,link,litpic,posttime,actionflag,postflag,chkflag,errflag) values(@planid,@title,@link,@litpic,@posttime,@actionflag,@postflag,@chkflag,@errflag)";

            QueryParameterCollection collection = new QueryParameterCollection();
            collection.Add("@planid", pageUrlData.PlanID, DbType.Int32);
            collection.Add("@title", pageUrlData.Title, DbType.String);
            collection.Add("@link", pageUrlData.Link, DbType.String);
            collection.Add("@litpic", pageUrlData.LitPic, DbType.String);
            collection.Add("@posttime", pageUrlData.PostTime, DbType.DateTime);
            collection.Add("@actionflag", pageUrlData.ActionFlag, DbType.Boolean);
            collection.Add("@postflag", pageUrlData.PostFlag, DbType.Boolean);
            collection.Add("@chkflag", pageUrlData.ChkFlag, DbType.Boolean);
            collection.Add("@errflag", pageUrlData.ErrFlag, DbType.Int32);

            return dataAccess.ExecuteNonQuery(sql, collection);
        }

        public static int UpdPageUrlData(cPageUrlData pageUrlData)
        {
            sql = "update list set actionflag=@actionflag,actionflag=@actionflag,postflag=@postflag,errflag=@errflag where id=@id";

            QueryParameterCollection collection = new QueryParameterCollection();
            collection.Add("@id", pageUrlData.ID, DbType.Int32);
            collection.Add("@actionflag", pageUrlData.ActionFlag, DbType.Boolean);
            collection.Add("@postflag", pageUrlData.PostFlag, DbType.Boolean);
            collection.Add("@chkflag", pageUrlData.ChkFlag, DbType.Boolean);
            collection.Add("@errflag", pageUrlData.ErrFlag, DbType.Int32);

            return dataAccess.ExecuteNonQuery(sql, collection);
        }

        public static int DelPageUrlData(int id)
        {
            sql = "delete list where id=@id";

            QueryParameterCollection collection = new QueryParameterCollection();
            collection.Add("@id", id, DbType.Int32);

            return dataAccess.ExecuteNonQuery(sql, collection);
        }

        #endregion

        #region plan ok
        public static List<cPlanData> GetPlanDataNameList(int g_id)
        {
            List<cPlanData> planDataList = new List<cPlanData>();

            sql = "select id,name from plan where g_id = @g_id";

            QueryParameterCollection collection = new QueryParameterCollection();
            collection.Add("@g_id", g_id, DbType.Int32);

            DataTable resultTable = dataAccess.ExecuteDataset(sql, collection).Tables[0];
            if(resultTable != null && resultTable.Rows.Count > 0)
            {
                foreach(DataRow row in resultTable.Rows)
                {
                    cPlanData planData = new cPlanData();
                    planData.id = TypeConverter.ObjectToInt(row["id"]);
                    planData.name = row["name"].ToString();

                    planDataList.Add(planData);
                }
            }

            return planDataList;
        }

        public static cPlanData GetPlanData(int id)
        {
            cPlanData planData = new cPlanData();

            sql = "select * from plan where id=@id";

            QueryParameterCollection collection = new QueryParameterCollection();
            collection.Add("@id", id, DbType.Int32);

            IDataReader reader = dataAccess.ExecuteReader(sql, collection);
            while(reader.Read())
            {
                planData.id = TypeConverter.StrToInt(reader["id"].ToString());
                planData.name = reader["name"].ToString();
                planData.rulecj = TypeConverter.StrToInt(reader["rulecj"].ToString());
                planData.rulemg = TypeConverter.StrToInt(reader["rulemg"].ToString());
                planData.rulepost = TypeConverter.StrToInt(reader["rulepost"].ToString());
                planData.ymdir = TypeConverter.StrToBool(reader["ymdir"].ToString(), false);
                planData.deldownfile = TypeConverter.StrToBool(reader["deldownfile"].ToString(), false);
                planData.forcepost = TypeConverter.StrToBool(reader["forcepost"].ToString(), false);
                planData.selfchk = TypeConverter.StrToBool(reader["selfchk"].ToString(), false);
                planData.chkmode = TypeConverter.StrToInt(reader["chkmode"].ToString());
                planData.prename = reader["prename"].ToString();
                planData.filepath = reader["filepath"].ToString();
                planData.postsort = TypeConverter.StrToBool(reader["postsort"].ToString(), false);
                planData.plandelay = TypeConverter.StrToInt(reader["plandelay"].ToString());
                planData.istime = TypeConverter.StrToBool(reader["istime"].ToString(), false);
                planData.autotime = reader["autotime"].ToString();
                planData.sn = TypeConverter.StrToInt(reader["sn"].ToString());
                planData.g_id = TypeConverter.StrToInt(reader["g_id"].ToString());
            }

            return planData;
        }

        public static int InsPlanData(cPlanData planData)
        {
            sql = "insert into plan(name,g_id) values(@name,@g_id)";

            QueryParameterCollection collection = new QueryParameterCollection();
            collection.Add("@name", planData.id, DbType.Int32);
            collection.Add("@g_id", planData.g_id, DbType.String);

            return dataAccess.ExecuteNonQuery(sql, collection);
        }

        public static int UpdPlanData(cPlanData planData)
        {
            sql = "update plan set name=@name,rulecj=@rulecj,rulemg=@rulemg,rulepost=@rulepost,ymdir=@ymdir,deldownfile=@deldownfile,forcepost=@forcepost,selfchk=@selfchk,chkmode=@chkmode,prename=@prename,filepath=@filepath,postsort=@postsort,plandelay=@plandelay,istime=@istime,autotime=@autotime,sn=@sn,g_id=@g_id where id=@id";

            QueryParameterCollection collection = new QueryParameterCollection();
            collection.Add("@id", planData.id, DbType.Int32);
            collection.Add("@name", planData.name, DbType.String);
            collection.Add("@rulecj", planData.rulecj, DbType.Int32);
            collection.Add("@rulemg", planData.rulemg, DbType.Int32);
            collection.Add("@rulepost", planData.rulepost, DbType.Int32);
            collection.Add("@ymdir", planData.ymdir, DbType.Boolean);
            collection.Add("@deldownfile", planData.deldownfile, DbType.Boolean);
            collection.Add("@forcepost", planData.forcepost, DbType.Boolean);
            collection.Add("@selfchk", planData.selfchk, DbType.Boolean);
            collection.Add("@chkmode", planData.chkmode, DbType.Int32);
            collection.Add("@prename", planData.prename, DbType.String);
            collection.Add("@filepath", planData.filepath, DbType.String);
            collection.Add("@postsort", planData.postsort, DbType.Boolean);
            collection.Add("@plandelay", planData.plandelay, DbType.Int32);
            collection.Add("@istime", planData.istime, DbType.Boolean);
            collection.Add("@autotime", planData.autotime, DbType.String);
            collection.Add("@sn", planData.sn, DbType.Int32);
            collection.Add("@g_id", planData.g_id, DbType.Int32);

            return dataAccess.ExecuteNonQuery(sql, collection);
        }

        public static int DelPlanData(int id)
        {
            sql = "delete plan where id=@id";

            QueryParameterCollection collection = new QueryParameterCollection();
            collection.Add("@id", id, DbType.Int32);

            return dataAccess.ExecuteNonQuery(sql, collection);
        }

        #endregion

        #region key ok
        public static List<cKeyData> GetKeyNameList(int g_id)
        {
            List<cKeyData> keyDataList = new List<cKeyData>();

            sql = "select id,name from rule_key where g_id = @g_id";

            QueryParameterCollection collection = new QueryParameterCollection();
            collection.Add("@g_id", g_id, DbType.Int32);

            DataTable resultTable = dataAccess.ExecuteDataset(sql, collection).Tables[0];
            if(resultTable != null && resultTable.Rows.Count > 0)
            {
                foreach(DataRow row in resultTable.Rows)
                {
                    cKeyData keyData = new cKeyData();
                    keyData.id = TypeConverter.ObjectToInt(row["id"]);
                    keyData.name = row["name"].ToString();

                    keyDataList.Add(keyData);
                }
            }

            return keyDataList;
        }

        public static cKeyData GetKeyData(int id)
        {
            cKeyData keyData = new cKeyData();

            sql = "select * from rule_key where id=@id";

            QueryParameterCollection collection = new QueryParameterCollection();
            collection.Add("@id", id, DbType.Int32);

            IDataReader reader = dataAccess.ExecuteReader(sql, collection);
            while(reader.Read())
            {
                keyData.id = TypeConverter.StrToInt(reader["id"].ToString());
                keyData.name = reader["name"].ToString();
                keyData.mg_title = reader["mg_title"].ToString();
                keyData.key_title = reader["key_title"].ToString();
                keyData.mg_content = reader["mg_content"].ToString();
                keyData.imgmark = TypeConverter.StrToInt(reader["imgmark"].ToString());
                keyData.imgmarkfilename = TypeConverter.StrToInt(reader["imgmarkfilename"].ToString());
                keyData.imgwz = TypeConverter.StrToInt(reader["imgwz"].ToString());
                keyData.imgalpha = TypeConverter.StrToInt(reader["imgalpha"].ToString());
                keyData.logow = TypeConverter.StrToInt(reader["logow"].ToString());
                keyData.logoh = TypeConverter.StrToInt(reader["logoh"].ToString());
                keyData.sn = TypeConverter.StrToInt(reader["sn"].ToString());
                keyData.g_id = TypeConverter.StrToInt(reader["g_id"].ToString());
            }

            return keyData;
        }

        public static int InsKeyData(cKeyData keyData)
        {
            sql = "insert into rule_key(name,g_id) values(@name,@g_id)";

            QueryParameterCollection collection = new QueryParameterCollection();
            collection.Add("@name", keyData.id, DbType.Int32);
            collection.Add("@g_id", keyData.g_id, DbType.String);

            return dataAccess.ExecuteNonQuery(sql, collection);
        }

        public static int UpdKeyData(cKeyData keyData)
        {
            sql = "update rule_key set name=@name,mg_title=@mg_title,key_title=@key_title,mg_content=@mg_content,imgmark=@imgmark,imgmarkfilename=@imgmarkfilename,imgwz=@imgwz,imgalpha=@imgalpha,logow=@logow,logoh=@logoh,sn=@sn,g_id=@g_id where id=@id";

            QueryParameterCollection collection = new QueryParameterCollection();
            collection.Add("@id", keyData.id, DbType.Int32);
            collection.Add("@name", keyData.name, DbType.String);
            collection.Add("@mg_title", keyData.mg_title, DbType.String);
            collection.Add("@key_title", keyData.key_title, DbType.String);
            collection.Add("@mg_content", keyData.mg_content, DbType.String);
            collection.Add("@imgmark", keyData.imgmark, DbType.Int32);
            collection.Add("@imgmarkfilename", keyData.imgmarkfilename, DbType.Int32);
            collection.Add("@imgwz", keyData.imgwz, DbType.Int32);
            collection.Add("@imgalpha", keyData.imgalpha, DbType.Int32);
            collection.Add("@logow", keyData.logow, DbType.Int32);
            collection.Add("@logoh", keyData.logoh, DbType.Int32);
            collection.Add("@sn", keyData.sn, DbType.Int32);
            collection.Add("@g_id", keyData.g_id, DbType.Int32);

            return dataAccess.ExecuteNonQuery(sql, collection);
        }

        public static int DelKeyData(int id)
        {
            sql = "delete rule_key where id=@id";

            QueryParameterCollection collection = new QueryParameterCollection();
            collection.Add("@id", id, DbType.Int32);

            return dataAccess.ExecuteNonQuery(sql, collection);
        }

        #endregion

        #region rule_post ok
        public static List<cPostData> GetPostNameList(int g_id)
        {
            List<cPostData> planNameList = new List<cPostData>();

            sql = "select id,name from rule_post where g_id = @g_id";

            QueryParameterCollection collection = new QueryParameterCollection();
            collection.Add("@g_id", g_id, DbType.Int32);

            DataTable resultTable = dataAccess.ExecuteDataset(sql, collection).Tables[0];
            if(resultTable != null && resultTable.Rows.Count > 0)
            {
                foreach(DataRow row in resultTable.Rows)
                {
                    cPostData postNameData = new cPostData();
                    postNameData.id = TypeConverter.ObjectToInt(row["id"]);
                    postNameData.name = row["name"].ToString();

                    planNameList.Add(postNameData);
                }
            }

            return planNameList;
        }

        public static cPostData GetPostData(int id)
        {
            cPostData postData = new cPostData();

            sql = "select * from rule_post where id=@id";

            QueryParameterCollection collection = new QueryParameterCollection();
            collection.Add("@id", id, DbType.Int32);

            IDataReader reader = dataAccess.ExecuteReader(sql, collection);
            while(reader.Read())
            {
                postData.id = TypeConverter.StrToInt(reader["id"].ToString());
                postData.name = reader["name"].ToString();
                postData.URLchk = reader["URLchk"].ToString();
                postData.URLpost = reader["URLpost"].ToString();
                postData.URLreply = reader["URLreply"].ToString();
                postData.isreply = TypeConverter.StrToBool(reader["isreply"].ToString(), false);
                postData.Len_t_s = TypeConverter.StrToInt(reader["Len_t_s"].ToString());
                postData.Len_t_l = TypeConverter.StrToInt(reader["Len_t_l"].ToString());
                postData.Len_c_s = TypeConverter.StrToInt(reader["Len_c_s"].ToString());
                postData.len_c_l = TypeConverter.StrToInt(reader["len_c_l"].ToString());
                postData.ptitle = reader["ptitle"].ToString();
                postData.pcontent = reader["pcontent"].ToString();
                postData.puser = reader["puser"].ToString();
                postData.ppsw = reader["ppsw"].ToString();
                postData.para1 = reader["para1"].ToString();
                postData.userlist = reader["userlist"].ToString();
                postData.paralist = reader["paralist"].ToString();
                postData.FTP = TypeConverter.StrToBool(reader["FTP"].ToString(), false);
                postData.FTPserver = reader["FTPserver"].ToString();
                postData.FTPport = TypeConverter.StrToInt(reader["FTPport"].ToString());
                postData.FTPuser = reader["FTPuser"].ToString();
                postData.FTPpsw = reader["FTPpsw"].ToString();
                postData.FTPpasv = TypeConverter.StrToBool(reader["FTPpasv"].ToString(), false);
                postData.ymupdir = TypeConverter.StrToBool(reader["ymupdir"].ToString(), false);
                postData.codepage = TypeConverter.StrToInt(reader["codepage"].ToString());
                postData.TransLang = TypeConverter.StrToInt(reader["TransLang"].ToString());
                postData.TransParas = reader["TransParas"].ToString();
                postData.pathfile = reader["pathfile"].ToString();
                postData.URLfile = reader["URLfile"].ToString();
                postData.pfiles = reader["pfiles"].ToString();
                postData.psurl = reader["psurl"].ToString();
                postData.isloginpost = TypeConverter.StrToBool(reader["isloginpost"].ToString(), false);
                postData.postcookies = reader["postcookies"].ToString();
                postData.postokcode = reader["postokcode"].ToString();
                postData.postaddurl = reader["postaddurl"].ToString();
                postData.isubb = TypeConverter.StrToBool(reader["isubb"].ToString(), false);
                postData.iscrlf = TypeConverter.StrToBool(reader["iscrlf"].ToString(), false);
                postData.sn = TypeConverter.StrToInt(reader["sn"].ToString());
                postData.g_id = TypeConverter.StrToInt(reader["g_id"].ToString());
            }

            return postData;
        }

        public static int InsPostData(cPostData postData)
        {
            sql = "insert into rule_post(name,g_id) values(@name,@g_id)";

            QueryParameterCollection collection = new QueryParameterCollection();
            collection.Add("@name", postData.id, DbType.Int32);
            collection.Add("@g_id", postData.g_id, DbType.String);

            return dataAccess.ExecuteNonQuery(sql, collection);
        }

        public static int UpdPostData(cPostData postData)
        {
            sql = "update rule_post set name=@name,URLchk=@URLchk,URLpost=@URLpost,URLreply=@URLreply,isreply=@isreply,Len_t_s=@Len_t_s,Len_t_l=@Len_t_l,Len_c_s=@Len_c_s,len_c_l=@len_c_l,ptitle=@ptitle,pcontent=@pcontent,puser=@puser,ppsw=@ppsw,para1=@para1,userlist=@userlist,paralist=@paralist,FTP=@FTP,FTPserver=@FTPserver,FTPport=@FTPport,FTPuser=@FTPuser,FTPpsw=@FTPpsw,FTPpasv=@FTPpasv,ymupdir=@ymupdir,codepage=@codepage,TransLang=@TransLang,TransParas=@TransParas,pathfile=@pathfile,URLfile=@URLfile,pfiles=@pfiles,psurl=@psurl,isloginpost=@isloginpost,postcookies=@postcookies,postokcode=@postokcode,postaddurl=@postaddurl,isubb=@isubb,iscrlf=@iscrlf,sn=@sn,g_id=@g_id where id=@id";

            QueryParameterCollection collection = new QueryParameterCollection();
            collection.Add("@id", postData.id, DbType.Int32);
            collection.Add("@name", postData.name, DbType.String);
            collection.Add("@URLchk", postData.URLchk, DbType.String);
            collection.Add("@URLpost", postData.URLpost, DbType.String);
            collection.Add("@URLreply", postData.URLreply, DbType.String);
            collection.Add("@isreply", postData.isreply, DbType.Boolean);
            collection.Add("@Len_t_s", postData.Len_t_s, DbType.Int32);
            collection.Add("@Len_t_l", postData.Len_t_l, DbType.Int32);
            collection.Add("@Len_c_s", postData.Len_c_s, DbType.Int32);
            collection.Add("@len_c_l", postData.len_c_l, DbType.Int32);
            collection.Add("@ptitle", postData.ptitle, DbType.String);
            collection.Add("@pcontent", postData.pcontent, DbType.String);
            collection.Add("@puser", postData.puser, DbType.String);
            collection.Add("@ppsw", postData.ppsw, DbType.String);
            collection.Add("@para1", postData.para1, DbType.String);
            collection.Add("@userlist", postData.userlist, DbType.String);
            collection.Add("@paralist", postData.paralist, DbType.String);
            collection.Add("@FTP", postData.FTP, DbType.Boolean);
            collection.Add("@FTPserver", postData.FTPserver, DbType.String);
            collection.Add("@FTPport", postData.FTPport, DbType.Int32);
            collection.Add("@FTPuser", postData.FTPuser, DbType.String);
            collection.Add("@FTPpsw", postData.FTPpsw, DbType.String);
            collection.Add("@FTPpasv", postData.FTPpasv, DbType.Boolean);
            collection.Add("@ymupdir", postData.ymupdir, DbType.Boolean);
            collection.Add("@codepage", postData.codepage, DbType.Int32);
            collection.Add("@TransLang", postData.TransLang, DbType.Int32);
            collection.Add("@TransParas", postData.TransParas, DbType.String);
            collection.Add("@pathfile", postData.pathfile, DbType.String);
            collection.Add("@URLfile", postData.URLfile, DbType.String);
            collection.Add("@pfiles", postData.pfiles, DbType.String);
            collection.Add("@psurl", postData.psurl, DbType.String);
            collection.Add("@isloginpost", postData.isloginpost, DbType.Boolean);
            collection.Add("@postcookies", postData.postcookies, DbType.String);
            collection.Add("@postokcode", postData.postokcode, DbType.String);
            collection.Add("@postaddurl", postData.postaddurl, DbType.String);
            collection.Add("@isubb", postData.isubb, DbType.Boolean);
            collection.Add("@iscrlf", postData.iscrlf, DbType.Boolean);
            collection.Add("@sn", postData.sn, DbType.Int32);
            collection.Add("@g_id", postData.g_id, DbType.Int32);

            return dataAccess.ExecuteNonQuery(sql, collection);
        }

        public static int DelPostData(int id)
        {
            sql = "delete rule_post where id=@id";

            QueryParameterCollection collection = new QueryParameterCollection();
            collection.Add("@id", id, DbType.Int32);

            return dataAccess.ExecuteNonQuery(sql, collection);
        }

        #endregion


    }
}
